import { t } from 'i18next'
import React, { useEffect, useState } from 'react'
import { fetchGetKisRequestInfo, IMeetingInfo } from '@/api/meetingSpace'
import { Wrap } from './style'
import { StyledBottom, IconNotFound, ErrorTips } from '../../style'
import { base64encode, utf16to8 } from '../../utils'
import { useRequest } from 'ahooks'

/** 智能签到表 */
const KisSign = ({ meetingInfo }: { meetingInfo: IMeetingInfo }) => {
	const [url, setUrl] = useState('')
	const [enableKis, setEnableKis] = useState<boolean | null>(null)
	const { data, loading } = useRequest(() => fetchGetKisRequestInfo(meetingInfo.meetingMoid))
	useEffect(() => {
		if (!loading) {
			if (data) {
				const { enableKis: k, token, signUrl } = data
				const _url = window.location.origin + signUrl
				setUrl(
					`${_url}?${base64encode(
						utf16to8(`meetingMoid=${meetingInfo.meetingMoid}&sso_cookie_key=${token}&refer=confSign`)
					)}`
				)
				setEnableKis(k === 1)
			} else {
				setEnableKis(false)
			}
		}
	}, [data, loading, meetingInfo])
	return (
		<Wrap>
			{enableKis === null ? (
				<></>
			) : enableKis ? (
				meetingInfo.isSigned ? (
					url && <iframe src={url} id="kis-record" title="kis-record" />
				) : (
					<ErrorTips>
						<IconNotFound />
						{t('会议中未开启智能签到')}
					</ErrorTips>
				)
			) : (
				<ErrorTips>
					<IconNotFound />
					{t('未配置KIS会议智能服务器，无法使用该功能')}。
				</ErrorTips>
			)}
			<StyledBottom />
		</Wrap>
	)
}
export default KisSign
